//
//  YFCalendarCell.m
//  CalendarDemo
//
//  Created by Winny Zhang on 2019/4/1.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFCalendarCell.h"
#import "FSCalendarExtensions.h"

@implementation YFCalendarCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
        CAShapeLayer *todayLayer = [[CAShapeLayer alloc] init];
        UIColor *todayColor =  UIColor.blackColor;// [UIColor colorWithRed:255/255.0 green:75/255.0 blue:100/255.0 alpha:1];
        [todayLayer setFillColor:todayColor.CGColor];
        todayLayer.actions = @{@"hidden":[NSNull null]};
        [self.contentView.layer insertSublayer:todayLayer below:self.titleLabel.layer];
        self.todayLayer = todayLayer;
        
        CAShapeLayer *selectionLayer = [[CAShapeLayer alloc] init];
        UIColor *fillColor = [UIColor colorWithRed:255/255.0 green:75/255.0 blue:100/255.0 alpha:1];
        [selectionLayer setFillColor:fillColor.CGColor];
        selectionLayer.actions = @{@"hidden":[NSNull null]}; // Remove hiding animation
        [self.contentView.layer insertSublayer:selectionLayer below:self.titleLabel.layer];
        self.selectionLayer = selectionLayer;
        
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor whiteColor];
        
        
        self.shapeLayer.hidden = YES;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = self.contentView.bounds;
    self.todayLayer.frame = self.contentView.bounds;
    self.selectionLayer.frame = self.contentView.bounds;

    self.todayLayer.borderColor = [UIColor redColor].CGColor;
    CGFloat todayDiameter = MIN(self.todayLayer.fs_height, self.todayLayer.fs_width);
    
    self.todayLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((self.contentView.fs_width - todayDiameter) * 0.5,
                                                                                 (self.contentView.fs_height - todayDiameter) * 0.5,
                                                                                 todayDiameter,
                                                                                 todayDiameter)].CGPath;

    if (self.selectionType == SelectionTypeMiddle) {
        
        self.selectionLayer.path = [UIBezierPath bezierPathWithRect:self.selectionLayer.bounds].CGPath;
        
    } else if (self.selectionType == SelectionTypeLeftBorder) {
        
        self.selectionLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.selectionLayer.bounds
                                                         byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)
                                                               cornerRadii:(CGSizeMake(self.selectionLayer.fs_height * 0.5,
                                                                                       self.selectionLayer.fs_height * 0.5))].CGPath;
        
    } else if (self.selectionType == SelectionTypeRightBorder) {
        
        self.selectionLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.selectionLayer.bounds
                                                         byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
                                                               cornerRadii:(CGSizeMake(self.selectionLayer.fs_height * 0.5,
                                                                                       self.selectionLayer.fs_height * 0.5))].CGPath;
        
    } else if (self.selectionType == SelectionTypeSingle) {
        
        CGFloat diameter = MIN(self.selectionLayer.fs_height, self.selectionLayer.fs_width);
        
        self.selectionLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((self.contentView.fs_width - diameter) * 0.5,
                                                                                     (self.contentView.fs_height - diameter) * 0.5,
                                                                                     diameter,
                                                                                     diameter)].CGPath;
    }
}



/**
 Setter 方法

 */
- (void)setSelectionType:(SelectionType)selectionType {
    if (_selectionType != selectionType) {
        _selectionType = selectionType;
        [self setNeedsLayout];
    }
}




@end
